
const XLSX = require('xlsx');
const log = require('../../utils/log');
const gameCfg = require('../../config/gameCfg');

// 读取Excel文件
const workbook = XLSX.readFile('../DT.xlsx');


class GameCommon {
    constructor() {
        // 获取第一个工作表
        const sheetName = workbook.SheetNames[0];
        //这个是所有的题库
        this.worksheet = workbook.Sheets[sheetName];

        const sheetName1 = workbook.SheetNames[1];
        this.worksheet_wt = workbook.Sheets[sheetName1];

        const sheetName2 = workbook.SheetNames[1];
        this.worksheet_zq = workbook.Sheets[sheetName2];

        //这个是主题赛文体类型题库
        const sheetName3 = workbook.SheetNames[1];
        this.worksheet_mz = workbook.Sheets[sheetName3];
    }

    static getInstance(){
        if(!GameCommon.instance){
            GameCommon.instance = new GameCommon();
            return GameCommon.instance;
        }else{
            return GameCommon.instance;
        }
    }

    //获取所有的题目数
    getAllTopic(){
        return XLSX.utils.sheet_to_json(this.worksheet);
    }

    //获取文体类型的题目数
    getWtTopicIds(){
        return  XLSX.utils.sheet_to_json( this.worksheet_wt);
    }

    //获取足球类型的题目数
    getZqTopicIds(){
        return  XLSX.utils.sheet_to_json( this.worksheet_zq);
    }

    // 获取民族类型的题目数
    getMzTopicIds(){
        return  XLSX.utils.sheet_to_json( this.worksheet_mz);
    }

    //根据题目id取一个值
    getItemById(targetId){
        let data = this.getAllTopic();
        const targetItem = data.find((item) => item.ID === targetId);
        return targetItem;
    }

    //获取主题类型的id 数组
    getTidsByType(type = -1){
        let ids = [];
        if(type === gameCfg.DWTYPE.DW_WENTI){
            ids = this.getWtTopicIds().map((item)=>item.ID);
        }else if(type === gameCfg.DWTYPE.DW_ZUQIU){
            ids = this.getZqTopicIds().map((item)=>item.ID);
        }else if(type === gameCfg.DWTYPE.DW_MINGZU){
            ids = this.getMzTopicIds().map((item)=>item.ID);
        }else{
            ids = this.getAllTopic().map((item)=>item.ID);
        }
        return ids;
    }

}

module.exports = GameCommon.getInstance();